/**
 * @(#)ProcessFormRequirementTag.java Nov 30, 2009
 *   
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved.   
 * TODO
 */

package com.wilmar.process.web.taglibs;

import javax.servlet.jsp.JspWriter;

import org.jbpm.api.ProcessEngine;
import org.jbpm.api.task.Task;

import com.wilmar.process.core.ProcessSystemDeclareKeys;
import com.wilmar.process.enumerate.EnumProcessBeanIdSet;
import com.wilmar.process.enumerate.EnumProcessSystemTagFixedVariableNameSet;
import com.wilmar.process.enumerate.EnumTagLibParameterScope;
import com.wilmar.process.enumerate.EnumTagLibResource;
import com.wilmar.process.exception.NoUserSessionException;

/**
 * @author MaKuBEX Email:makubexnet@gmail.com
 * @version 1.0.4
 */
public class ProcessFormRequirementTag extends ProcessTagSupport {
	private String keygenerator;
	private String processInstanceKey;

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 5855090714092537946L;

	/**
	 * @see org.springframework.web.servlet.tags.RequestContextAwareTag#doStartTagInternal()
	 */
	protected int doStartTagInternal() throws Exception {
		StringBuffer html = new StringBuffer();
		html.append("<script type=\"text/javascript\">\n");
		html.append("DEFAULT_TASKINSTANCE_FORM_SUBMIT_URL = \"");
		html
				.append(ProcessSystemDeclareKeys.DEFAULT_TASK_INSTANCE_COMPLETE_ACTION_URL);
		html.append("\";\n");
		html.append("PROCESS_INFORMATION_OUTCOME_ELEMENT_ID = \"");
		html
				.append(EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_TASK_INSTANCE_OUTCOME
						.getValue());
		html.append("\";\n");
		html.append("</script>\n");
		html
				.append("<input type=\"hidden\" id=\""
						+ EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_TASK_INSTANCE_OUTCOME
								.getValue()
						+ "\" name=\""
						+ EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_TASK_INSTANCE_OUTCOME
								.getValue() + "\" value=\"\"/>\n");
		if (this.keygenerator != null && this.keygenerator.trim().length() != 0) {
			html
					.append("<input type=\"hidden\" id=\""
							+ EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_PROCESS_INSTANCE_KEYGENERATOR
									.getValue()
							+ "\" name=\""
							+ EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_PROCESS_INSTANCE_KEYGENERATOR
									.getValue() + "\" value=\""
							+ this.keygenerator + "\"/>\n");
		}
		this.generateProcessInformationHtmlCode(html);
		JspWriter outer = super.getOut();
		outer.write(html.toString());
		return SKIP_BODY;
	}

	protected void generateProcessInformationHtmlCode(StringBuffer html) throws NoUserSessionException {
		if (this.processInstanceKey != null
				&& this.processInstanceKey.trim().length() != 0) {
			html
					.append("<input type=\"hidden\" id=\""
							+ EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_PROCESS_INSTANCE_KEY
									.getValue()
							+ "\" name=\""
							+ EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_PROCESS_INSTANCE_KEY
									.getValue() + "\" value=\""
							+ this.processInstanceKey + "\"/>\n");
		}
		String tiid = super
				.getAttribute(EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_TASK_INSTANCE_ID);
		if (tiid != null) {
			ProcessEngine processEngine = (ProcessEngine) super
					.getBean(EnumProcessBeanIdSet.PROCESS_SYSTEM_CORE_POCESSENGINE);
			Task task = processEngine.getTaskService().getTask(tiid);
			if (task == null) {
				tiid = super
						.getAttribute(
								EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_TASK_INSTANCE_ID,
								EnumTagLibParameterScope.REQUEST_ATTRIBUTE);
			}
			html
					.append("<input type=\"hidden\" id=\""
							+ EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_TASK_INSTANCE_ID
									.getValue()
							+ "\" name=\""
							+ EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_TASK_INSTANCE_ID
									.getValue() + "\" value=\"" + tiid
							+ "\"/>\n");
		}
		String piid = super
				.getAttribute(EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_PROCESS_INSTANCE_ID);
		if (piid != null) {
			html
					.append("<input type=\"hidden\" id=\""
							+ EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_PROCESS_INSTANCE_ID
									.getValue()
							+ "\" name=\""
							+ EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_PROCESS_INSTANCE_ID
									.getValue() + "\" value=\"" + piid
							+ "\"/>\n");
		}
		String pdid = super
				.getAttribute(EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_PROCESS_DEFINITION_ID);
		if (pdid != null) {
			html
					.append("<input type=\"hidden\" id=\""
							+ EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_PROCESS_DEFINITION_ID
									.getValue()
							+ "\" name=\""
							+ EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_PROCESS_DEFINITION_ID
									.getValue() + "\" value=\"" + pdid
							+ "\"/>\n");
		}
		String pdkey = super
				.getAttribute(EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_PROCESS_DEFINITION_KEY);
		if (pdkey != null) {
			html
					.append("<input type=\"hidden\" id=\""
							+ EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_PROCESS_DEFINITION_KEY
									.getValue()
							+ "\" name=\""
							+ EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_PROCESS_DEFINITION_KEY
									.getValue() + "\" value=\"" + pdkey
							+ "\"/>\n");
		} else {
			html
					.append("<input type=\"hidden\" id=\""
							+ EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_PROCESS_DEFINITION_KEY
									.getValue()
							+ "\" name=\""
							+ EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_PROCESS_DEFINITION_KEY
									.getValue() + "\"/>\n");
		}
		html
				.append("<link href=\""
						+ super.getContextPath()
						+ ProcessSystemDeclareKeys.PAGE_STYLE_DIR
						+ ProcessSystemDeclareKeys.SLASH
						+ "confirm_window.css\" type=\"text/css\" rel=\"stylesheet\" />\n");
		html
				.append("<div id=\""
						+ ProcessSystemDeclareKeys.TASKINSTANCE_FORM_COMMIT_CONFIRM_DIV
						+ "\" class=\"confirm_window\" style=\"display:none; cursor: default\">\n");
		html
				.append("<span id=\""
						+ ProcessSystemDeclareKeys.TASKINSTANCE_FORM_COMMIT_CONFIRM_DIV_CONTENT
						+ "\"></span>\n");
		html
				.append("<div align=\"right\"><input type=\"submit\" id=\""
						+ ProcessSystemDeclareKeys.TASKINSTANCE_FORM_COMMIT_CONFIRM_DIV_SUBMIT_BTN
						+ "\" value=\""
						+ super
								.getText(EnumTagLibResource.TASKINSTANCE_FORM_SUBMIT_BUTTON)
						+ "\" onclick=\"doBlockForm();\" />\n");
		html
				.append("<input type=\"button\" id=\""
						+ ProcessSystemDeclareKeys.TASKINSTANCE_FORM_COMMIT_CONFIRM_DIV_CANCEL_BTN
						+ "\" value=\""
						+ super
								.getText(EnumTagLibResource.TASKINSTANCE_FORM_CANCEL_BUTTON)
						+ "\" onclick=\"doUnBlock();\"/></div>\n");
		html.append("</div>\n");
		html
				.append("<div id=\""
						+ ProcessSystemDeclareKeys.TASKINSTANCE_FORM_COMMIT_COMMITING_MESSAGE_DIV
						+ "\" style=\"display:none;\">\n");
		html
				.append("<p><img src=\""
						+ super.getContextPath()
						+ ProcessSystemDeclareKeys.PAGE_ICON_DIR
						+ ProcessSystemDeclareKeys.SLASH
						+ "loading.gif\" />"
						+ super
								.getText(EnumTagLibResource.TASKINSTANCE_FROM_COMMITING_MESSAGE)
						+ "</p>");
		html.append("</div>");
	}

	/**
	 * @return the keygenerator
	 */
	public String getKeygenerator() {
		return keygenerator;
	}

	/**
	 * @param keygenerator
	 *            the keygenerator to set
	 */
	public void setKeygenerator(String keygenerator) {
		this.keygenerator = keygenerator;
	}

	/**
	 * @return the processInstanceKey
	 */
	public String getProcessInstanceKey() {
		return processInstanceKey;
	}

	/**
	 * @param processInstanceKey
	 *            the processInstanceKey to set
	 */
	public void setProcessInstanceKey(String processInstanceKey) {
		this.processInstanceKey = processInstanceKey;
	}

}
